
// 把一个list转为tree
function arrToTree (arr) {
  let res = []
  const map = arr.reduce((res, v) => {
    res[v.id] = v
    return res
  }, {})
  arr.forEach(item => {
    let parent = map[item.parentId]
    if (parent) {
      parent.children = parent.children || []
      parent.children.push(item)
    } else {
      res.push(parent)
    }
  });
  return res
}

// 把一个tree转为list
function treeToArr (tree) {
  let res = []
  function flatFun (tree) {
    tree && tree.forEach(v => {
      res.push(v)
      v.children && v.children.length > 0 && flatFun(v.children)
      delete v.children
    })
  }
  flatFun(tree)
  return res
}

// 输出树上某一个节点的所有父节点数组
function findPareArr (tree, currentId) {
  let res = []
  function find (arr, temp = []) {
    for (const item of arr) {
      if (item.children) {
        find(item.children, temp.concat(item.id))
      } else {
        if (item.id === currentId) {
          res = temp
        }
      }
    }
  }
  find(tree)
  return res
}
